#include<iostream>
using namespace std;
void heap_operate(int* a, int n, int idx)
{
	int ma = idx;
	int lson = idx * 2 + 1;
	int rson = idx * 2 + 2;
	if (lson<n && a[lson]>a[ma])ma = lson;
	if (rson<n && a[rson]>a[ma])ma = rson;
	if (ma != idx) {
		swap(a[ma], a[idx]);
		heap_operate(a, n, ma);
	}
}
void heap_sort(int* a, int n)
{
	for (int i = n / 2 - 1; i >= 0; i--) {
		heap_operate(a, n, i);
	}
	for (int i = n - 1; i > 0; i--) {
		swap(a[i], a[0]);
		heap_operate(a, i, 0);
	}
}
int main()
{
	int a[] = { 11,32,4,2,23,-1,20 };
	int n = sizeof(a) / sizeof(a[0]);
	for (int i = 0; i < n; i++) {
		cout << a[i] << " ";
	}
	cout << endl;
	heap_sort(a, n);
	for (int i = 0; i < n; i++) {
		cout << a[i] << " ";
	}
	cout << endl;
	return 0;
}